﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace XRpgLibrary.InterfaceClasses
{
    public class InterfaceManager : DrawableGameComponent, IGameDrawableMenu, IGameUpdateableMenu
    {
        #region Events
        public event PauseStateChangedEventHandler PauseStateChanged; 
        #endregion

        #region Fields
        private Stack<BaseMenuComponent> menus;
        private RpgGame game;
        private SpriteBatch spriteBatch;
        private Rectangle screenRect;
        #endregion


        public Stack<BaseMenuComponent> Menus
        {
            get
            {
                if (menus == null)
                    menus = new Stack<BaseMenuComponent>();

                return menus;
            }
        }

        public InterfaceManager(RpgGame game)
            : base(game)
        {
            this.game = game;
            this.spriteBatch = game.componentSpriteBatch;
            this.screenRect = game.ScreenRectangle;
        }

        #region Convenience Methods

        public ConversationBox Textbox(string text, params object[] args)
        {
            ConversationBox convo = new ConversationBox(this.game, string.Format(text, args));

            if (!Menus.Contains(convo))
                Menus.Push(convo);

            convo.Visible = true;
            convo.Enabled = true;

            return convo;
        }

        #endregion

        public void PushMenu(BaseMenuComponent menu)
        {
            //if (!Menus.Contains(menu))
                Menus.Push(menu);

            menu.Visible = true;
            menu.Enabled = true;
        }

        public void PopMenu()
        {
            BaseMenuComponent menu = Menus.Pop();

            if (menu != null)
            {
                menu.Visible = false;
                menu.Enabled = false;
                menu.IsAlive = false;
            }
        }

        //public void DeactivateMenu(BaseMenuComponent menu)
        //{
        //    if (Menus.Contains(menu))
        //        Menus.Remove(menu);

        //    menu.Visible = false;
        //    menu.Enabled = false;
        //    menu.IsAlive = false;
        //}

        protected virtual void OnPauseStateChanged(bool isPaused)
        {
            foreach (BaseMenuComponent m in Menus)
                m.OnPauseStateChanged(isPaused);
        }

        #region Drawing and Updating Interfaces

        public bool IsAlive { get; set; }

        public override void Draw(GameTime gameTime)
        {
            foreach (BaseMenuComponent m in Menus)
                m.Draw(gameTime);
        }


        public override void Update(GameTime gameTime)
        {
            if (InputHandler.KeyReleased(GameCommands.ActivateMainMenu))
            {
                if (Menus.Count > 0)
                    Menus.Pop();
            }

            foreach (BaseMenuComponent m in Menus)
                m.Update(gameTime);
        } 

        #endregion
    }
}
